﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台用户管理</title>
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/static/assets/module/admin.css?v=318"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">手机号码:</label>
                        <div class="layui-input-inline">
                            <input name="username" class="layui-input" placeholder="输入手机号码"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">用户昵称:</label>
                        <div class="layui-input-inline">
                            <input name="nickName" class="layui-input" placeholder="输入用户昵称"/>
                        </div>
                    </div>
                    <!--<label class="layui-form-label">状态:</label>-->
                    <!--    <div class="layui-input-inline">-->

                    <!--        <select name="status" lay-verType="tips" >-->
                    <!--            <option value="">选择</option>-->
                    <!--            <option value="0">正常</option>-->
                    <!--            <option value="1">禁止</option>-->
                               
                    <!--        </select>-->
                    <!--    </div>-->
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="userTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTbBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<!--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
<!--       <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reset">重置密码</a>-->
</script>
<!-- 表格状态列 -->
<script type="text/html" id="userTbState">
    <input type="checkbox" lay-filter="userTbStateCk" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.status==0?'checked':''}} style="display: none;"/>
    <p style="display: none;">{{d.status==0?'正常':'锁定'}}</p>
</script>

<script type="text/html" id="img">
    <img src="{{d.portraitUri}}" width="50" height="50">
</script>

<script type="text/html" id="haoyou">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="haoyouCount">{{d.friendCount}}</a>
</script>

<script type="text/html" id="group">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="groupCount">{{d.groupCount}}</a>
</script>
<script type="text/html" id="adminEditDialog">
    <form id="adminEditDialog" lay-filter="adminEditDialog" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label" id="haoyouCount"></label>
            <div>
                <div style="padding-left: 100px;padding-top: 10px;font-size: 18px">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>昵称</th>
                            <th>头像</th>
                            <th>手机号码</th>
                        </tr>
                        </thead>
                        <tbody id="list">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
       <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script type="text/html" id="gruopEditDialog">

        <div class="layui-form-item">

            <div>
                <div style="font-size: 18px">
                    <table class="layui-table">
                        <thead>
                        <tr>
                            <th>群名称</th>
                            <th>头像</th>
                            <th>群数量</th>
                        </tr>
                        </thead>
                        <tbody id="list2">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>

</script>

<!-- 表单弹窗 -->
<!-- js部分 -->
<script type="text/javascript" src="/static/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/static/assets/js/common.js?v=318"></script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        /* 渲染表格 */
        var insTb = table.render({
            elem: '#userTable',
            url: '/index/User/getUserinfo',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                // '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},

                {field: 'nickname', title: '名称', sort: true},
                {field: 'name', title: '姓名', sort: true},
                {field: 'phone', title: '手机号', sort: true},
                {field: 'dailiName', title: '推荐人', sort: true},
                {field: 'portraitUri', title: '图像', templet: '#img', sort: true, width: 100},
                {field: 'gender', title: '性别'},
                {field: 'friendCount', title: '好友数量',templet:"#haoyou",sort: true,sort: true},
                {field: 'groupCount', title: '群数量',templet:"#group", sort: true,sort: true},
                {field: 'status', title: '状态' , templet:"#userTbState"},
                 {field: 'is_merchant', title: '类型' },
                {
                    field: 'createdAt', title: '创建时间', sort: true
                },
                {
                    field: 'updatedAt', title: '最后登录时间', sort: true
                },

                {title: '操作', toolbar: '#userTbBar', align: 'center', minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(userTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(userTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                adminEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            } else if (obj.event === 'reset') { // 重置密码
                resetPsw(obj);
            } else if(obj.event == 'haoyouCount'){ // 好友数量
                haoyouCount(obj);
            }else if(obj.event === 'groupCount'){ // 群数量
                groupCount(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(userTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('userTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.userId;
                });
                doDel({ids: ids});
            }
        });


        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadindexx = layer.load(2);

                $.post('/index/Admin/delAdmin', {
                    id: obj.data.id,

                }, function (res) {
                    layer.close(loadindexx);
                    if (res.code === 1) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 修改用户状态 */
        form.on('switch(userTbStateCk)', function (obj) {
            var loadindexx = layer.load(2);
            $.post('/index/User/changeUserState', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (res) {

                layer.close(loadindexx);
                if (res.code === 1) {
                    layer.msg(res.msg, {icon: 1});
                } else {
                    layer.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            }, 'json');
        });

        /* 重置密码 */
        function resetPsw(obj) {
            layer.confirm('确定要重置“' + obj.data.nickName + '”的登录密码吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadindexx = layer.load(2);
                $.get('../../json/ok.json', {
                    userId: obj.data.userId
                }, function (res) {
                    layer.close(loadindexx);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }
        // 好友数量
        function  haoyouCount(mData) {
            admin.open({
                type: 1,
                title: '好友列表',
                area: ['1280px','800px'],
                content: $('#adminEditDialog').html(),
                success: function (layero, dindexx) {
                    // 回显表单数据
                    form.val('userEditForm', mData);
                    // 表单提交事件
                    form.on('submit(userEditSubmit)', function (data) {

                        var loadindexx = layer.load(2);
                        $.post('', data.field, function (res) {  //

                        }, 'json');
                        return false;
                    });
                    // 渲染多选下拉框
                    $.post('/index/User/getHaoyou', {id:mData.data.id}, function (res) {
                        console.log(res.data);
                        $("#haoyouCount").text("好友数量"+res.count);
                        if(res.data){
                            var data = res.data;

                            data.forEach(function (a,c) {

                                var html = "<tr><td>"+a.nickname+"</td><td><img src="+a.portraitUri+"></td><td>"+a.phone+"</td></tr>";
                                $("#list").append(html);
                            })
                        }
                    }, 'json')

                }
            });
        }
        // 群列表
        function  groupCount(mData) {
            admin.open({
                type: 1,
                title: '群列表',
                area: ['880px','600px'],
                content: $('#gruopEditDialog').html(),
                success: function (layero, dindexx) {


                    // 表单提交事件
                    form.on('submit(userEditSubmit)', function (data) {

                        var loadindexx = layer.load(2);
                        $.post('/index/Admin/addAdminuser', data.field, function (res) {  //

                        }, 'json');
                        return false;
                    });
                    // 渲染多选下拉框
                    $.post('/index/User/getGropCount', {id:mData.data.id}, function (res) {
                        console.log(res.data);
                        $("#groupCount").text("群列表数量"+res.count);
                        if(res.data){
                            var data = res.data;

                            data.forEach(function (a,c) {

                                var html = "<tr><td>"+a.nickname+"</td><td><img src="+a.portraitUri+"></td><td>"+a.memberCount+"</td></tr>";
                                $("#list2").append(html);
                            })
                        }
                    }, 'json')

                }
            });
        }
    });
</script>
</body>
</html>
